<template>
  <vxe-modal
    ref="commonSelectModal"
    v-model="visible"
    :resize="true"
    width="1024"
    height="600"
    title="分配用户"
    :show-zoom="true"
    :destroy-on-close="false"
    :showFooter="true"
    @close="cancel"
  >
    <div class="common-select-container">
      <div class="searchBox">
        <el-form :inline="true" :model="search" ref="searchForm">
          <el-form-item
            v-for="item in searchColumns"
            :key="item.field"
            :label="item.name"
            :prop="item.column"
          >
            <el-input v-model="search[item.field]" placeholder="请输入内容"></el-input>
          </el-form-item>
          <el-form-item style="margin-top: -2px">
            <el-button type="primary" @click="tableSearch">查询</el-button>
            <el-button type="info" @click="search = {}, tableSearch()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <qm-table
        v-if="visible"
        ref="qmTable"
        :urls="{ query: '/admin/sysDepartment/addUsers/query' }"
        :showMenu="false"
        :showToolbar="false"
        :showCheckbox="multiple"
        :showRadio="!multiple"
        :options="calOptions"
        :pagerOptions="{ pageSize: 5 }"
        :beforeQuery="beforeQuery"
      >
        <qm-table-column
          v-for="item in columns"
          :key="item.field"
          :field="item.field"
        >{{ item.name }}</qm-table-column>
      </qm-table>
    </div>

    <template v-slot:footer>
      <el-row>
        <el-col :span="24" style="text-align: right;">
          <el-button type="primary" @click="returnResult">确定</el-button>
          <el-button type="info" @click="cancel">关闭</el-button>
        </el-col>
      </el-row>
    </template>
  </vxe-modal>
</template>

<script>
export default {
  name: "set-users",
  props: {
    url: String,
    multiple: Boolean,
    value: Boolean,
    onBeforeQuery: Function,
  },
  data() {
    return {
      searchColumns: [
        { name: "登录账号", field: "loginId" },
        { name: "用户名", field: "userName" },
      ],
      columns: [
        { name: "登录账号", field: "loginId" },
        { name: "用户名", field: "userName" },
      ],
      search: {},
      visible: false,
    };
  },
  computed: {
    calOptions() {
      if (this.multiple) {
        return { checkboxConfig: { highlight: true, trigger: "row" } };
      }
      return {
        checkboxConfig: { highlight: false },
        radioConfig: { highlight: true, trigger: "row" },
      };
    },
  },
  watch: {
    value(visible) {
      this[visible ? "show" : "hide"]();
    },
  },
  methods: {
    tableSearch() {
      let params = {
        start: 0,
        query: {},
      };
      params.query = _.extend({}, this.search);
      this.$refs["qmTable"].fetchListData(params);
    },
    beforeQuery(params) {
      if (this.onBeforeQuery) {
        params.query = _.extend({}, params.query, this.search);
        this.onBeforeQuery(params);
      }
    },
    show() {
      this.visible = true;
    },
    hide() {
      this.visible = false;
    },
    cancel() {
      this.$emit("cancel");
    },
    returnResult() {
      let rows = this.$refs["qmTable"].getSelectedRows();
      this.$emit("success", rows);
    },
  },
};
</script>

<style>
</style>